app.use("/download", function (req, res) {
    var path = decodeURI(req.path.replace("_s_", "#"));
    res.download(__dirname + "/source" + path);
    console.log(req.body);
});


app.use("/sourceZY", function (req, res) {
    var path = decodeURI(req.path);
    console.log(req.body);
    res.download(__dirname + "/works" + path);
});

//获取文件图片
app.post("/filelist", function (req, res) {

    var sourcePath = __dirname + "/source/";
    fs.readdir(sourcePath, { withFileTypes: true }, function (err, files) {
        console.log("files:" + files)
        // console.log(files)
        if (!err && files.length != 0) {

            var fileList = [];
            // console.dir(path.parse(files[0].name));  
            for (let i = 0; i < files.length; i++) {
                // console.log(files[i].name);
                // console.log("files[i]:"+ i);

                let info = path.parse(files[i] || '');
                // console.log(info)
                // var fname = info.name;

                // ext 文件扩展名
                let type = typeDetect.isPic(info.ext) ? "pic" : "file";
                console.log(info.base);
                fs.stat("./source/" + info.base, function (err, stats) {
                    if (err) {
                        console.log("fs.stat err");
                    }
                    else {
                        fileList.push({
                            brief: info.name,
                            pathname: "/sourcepic/" + encodeURI(info.base),
                            down: "/download/" + encodeURI(info.base),
                            type: type,
                            pathnamesort: "./source/" + encodeURI(info.base),
                            ctime: stats.ctime,
                            stime: stats.stime,
							lastModified: ''
                        });
                    }

                    if (fileList.length == files.length) {
                        res.send({
                            data: fileList
                        });
                    }
                });
                // fileList.push({
                //     brief:info.name,
                //     pathname:"/sourcepic/"+encodeURI(info.base),
                //     down:"/download/"+encodeURI(info.base),
                //     type:type
                // });
            }
            // res.send({
            //     data:fileList
            // });
        }
        else {
            res.send({
                data: null
            });
        }

    });
});